package com.qf.blog.core.utils;

import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DataSourceUtils {

    static Properties properties = null;
    private static DruidDataSource dataSource = null;

    // 把配置文件中的内容加载properties中
    // 在第一次加载这个类的时候就创建连接池
    static {
        InputStream ips = DataSourceUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        properties = new Properties();
        try {
            properties.load(ips);
            dataSource = new DruidDataSource();
            dataSource.setPassword(properties.getProperty("jdbc.password"));
            dataSource.setUsername(properties.getProperty("jdbc.username"));
            dataSource.setUrl(properties.getProperty("jdbc.url"));
            dataSource.setDriverClassName(properties.getProperty("jdbc.driver"));
            dataSource.setInitialSize(20);
            dataSource.setMaxActive(50);
            dataSource.setMaxWait(6000);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ips != null) {
                try {
                    ips.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static DataSource getDataSource() {
        return dataSource;
    }
}
